package com.soulmate.auth.authencation.filter;

import com.soulmate.auth.token.TelephoneCodeAuthenticationToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 手机号验证码验证
 * @author huaiping
 */
@Slf4j
public class TelephoneCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    /**
     * 电话号码
     */
    public static final String SPRING_SECURITY_PHONE_NUMBER_KEY = "telephone";
    /**
     * code
     */
    public static final String SPRING_SECURITY_PHONE_CODE_KEY = "code";
    private static final String SPRING_SECURITY_PHONE_URL = "/user/login/phone-code";
    private final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER =
            new AntPathRequestMatcher(SPRING_SECURITY_PHONE_URL, "POST");

    /**
     * 仅post方式
     */
    private final static boolean POST_ONLY = true;

    @Autowired
    @Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);
    }

    public TelephoneCodeAuthenticationFilter() {
        super(SPRING_SECURITY_PHONE_URL);
    }

    public TelephoneCodeAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(SPRING_SECURITY_PHONE_URL, authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        if (POST_ONLY && ! HttpMethod.POST.matches(request.getMethod())) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }
        String telephone = obtainTelephone(request);
        telephone = Objects.nonNull(telephone) ? telephone.trim() : "";

        String code = obtainTelephoneCode(request);
        code = Objects.nonNull(code) ? code.trim() : "";
        TelephoneCodeAuthenticationToken authRequest = TelephoneCodeAuthenticationToken.unauthenticated(telephone, code);
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    /**
     * 获取电话
     *
     * @param request 要求
     * @return {@link String}
     */
    @Nullable
    protected String obtainTelephone(HttpServletRequest request) {
        return request.getParameter(SPRING_SECURITY_PHONE_NUMBER_KEY);
    }

    /**
     * 获取手机验证码
     *
     * @param request 要求
     * @return {@link String}
     */
    @Nullable
    protected String obtainTelephoneCode(HttpServletRequest request) {
        return request.getParameter(SPRING_SECURITY_PHONE_CODE_KEY);
    }

    protected void setDetails(HttpServletRequest request, TelephoneCodeAuthenticationToken authRequest) {
        authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
    }
}
